#include <iostream>
#include "spdlog/spdlog.h"
#include "udp_server.h"

using namespace std;
using namespace safe_udp;

constexpr char SERVER_FILE_PATH[] = "/work/files/server_files/";

int main(int argc, char* argv[]) {
    if (argc < 3) {
        spdlog::info("Please provide a port number and receive window");
        spdlog::error("Please provide format: <server-port> <receiver-window>");
        exit(1);
    }
    int sfd = 0;
    int port_num = atoi(argv[1]);
    int recv_window = atoi(argv[2]);
    char* message_recv = nullptr;

    UdpServer udp_server;
    udp_server.rwnd_ = recv_window;
    sfd = udp_server.StartServer(port_num);
    message_recv = udp_server.GetRequest(sfd);

    string file_name = string(SERVER_FILE_PATH) + string(message_recv);
    if (udp_server.Openfile(file_name)) {
        udp_server.StartFileTransfer();
    } else {
        udp_server.SendError();
    }

    return 0;
}